package com.ly.hotel.view.floatWindows;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.WindowManager;
import android.view.ViewTreeObserver.OnPreDrawListener;

public class ShuaKafloatView extends View {
	static AnimationDrawable frameAnimation = null;
	Context mContext = null;
	/* 定义一个Drawable对象 */
	Drawable mBitAnimation = null;
	WindowManager wm = (WindowManager) getContext().getApplicationContext().getSystemService("window");
	WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
	static OnPreDrawListener opdl;

	public ShuaKafloatView(Context context) {
		super(context);

		mContext = context;
		/* 实例化AnimationDrawable对象 */
		frameAnimation = new AnimationDrawable();

		/* 装载资源 */
		// 这里用一个循环了装载所有名字类似的资源
		// 如“a1.......15.png”的图片
		// 这个方法用处非常大
		for (int i = 1; i < 4; i++) {
			int id = getResources().getIdentifier("a" + i, "drawable", mContext.getPackageName());
			mBitAnimation = getResources().getDrawable(id);
			/* 为动画添加一帧 */
			// 参数mBitAnimation是该帧的图片
			// 参数100是该帧显示的时间,按毫秒计算
			frameAnimation.addFrame(mBitAnimation, 200);

		}
		frameAnimation.setOneShot(false); /* 设置播放模式是否循环false表示循环而true表示不循环 */
		this.setBackgroundDrawable(frameAnimation);

		opdl = new OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				frameAnimation.start();
				return true;
			}
		};
		this.getViewTreeObserver().addOnPreDrawListener(opdl);
	}

	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
}
